//
//  KLWeekDetailViewController.m
//  KunLun
//
//  Created by Mac on 2017/12/27.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLWeekDetailViewController.h"
#import "KLWeekTableViewCell.h"
#import "KLSuggestModel.h"
#import "KLWeekSuggestViewController.h"
@interface KLWeekDetailViewController ()
@property(nonatomic, strong) NSMutableArray                     *dataSouce;
@property(nonatomic, strong) UIView                             *header;
@property(nonatomic, strong) STButton                           *cotactButton;
@property(nonatomic, strong) KLUserModel                        *userModel;
@end

@implementation KLWeekDetailViewController


#pragma mark --vc 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = self.tableView.backgroundColor;
    self.title = @"病人信息";
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendWeekPeopleRequest];
        [self sendMyInfoRequest];
    }];
    self.tableView.st_noreslutView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, self.header.height + 70, UIScreenWidth, 400) title:@"暂无指导" buttonTitle:nil buttonHandle:^(NSString *titleString) {
        
    }];
    [self.tableView.mj_header beginRefreshing];

    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 300)];
    header.backgroundColor = [UIColor whiteColor];
    

    STButton * iconButton = [[STButton alloc] initWithFrame:CGRectMake(30, 10, 80, 80)
                                                      title:nil
                                                 titleColor:nil
                                                  titleFont:0
                                               cornerRadius:40
                                            backgroundColor:nil
                                            backgroundImage:nil
                                                      image:nil];
    [iconButton sd_setBackgroundImageWithURL:[NSURL URLWithString:self.model.header] forState:UIControlStateNormal];
    [header addSubview:iconButton];
    
    STLabel * nameLable = [[STLabel alloc] initWithFrame:CGRectMake(iconButton.right  +10, iconButton.top  +20, 200, 20)
                                                    text:self.model.name
                                               textColor:[UIColor blackColor]
                                                    font:17
                                             isSizetoFit:NO
                                           textAlignment:NSTextAlignmentLeft];
    [header addSubview:nameLable];
    
    STLabel * typeLable = [[STLabel alloc] initWithFrame:CGRectMake(iconButton.right  +10, nameLable.bottom  +5, 200, 16)
                                                    text:[NSString stringWithFormat:@"年龄:%@",self.model.age]
                                               textColor:KL_firstTextColor
                                                    font:14
                                             isSizetoFit:NO
                                           textAlignment:NSTextAlignmentLeft];
    [header addSubview:typeLable];
    

    
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, iconButton.bottom + 20,  UIScreenWidth, 10)];
    line.backgroundColor = KL_backgroundColor;
    [header addSubview:line];
    
    
    STLabel * desLable = [[STLabel alloc] initWithFrame:CGRectMake(self.insetX, line.bottom, UIScreenWidth, 44)
                                                   text:@"症状:"
                                              textColor:[UIColor blackColor]
                                                   font:18
                                            isSizetoFit:NO
                                          textAlignment:NSTextAlignmentLeft];
    [header addSubview:desLable];
    
    STLabel * mydesLable = [[STLabel alloc] initWithFrame:CGRectMake(self.insetX, desLable.bottom  +10, UIScreenWidth - 2 * self.insetX, 20)
                                                     text:self.model.details
                                                textColor:KL_firstTextColor
                                                     font:15
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [header addSubview:mydesLable];
    [NSString lableAutoAdjustheightWithLabel:mydesLable];
    
    UIView * graView = [[UIView alloc] initWithFrame:CGRectMake(0, mydesLable.bottom + 20,  UIScreenWidth, 10)];
    graView.backgroundColor = KL_backgroundColor;
    [header addSubview:graView];
    
    CGFloat top = graView.bottom +10;
    for (int i =0; i < self.model.pictures.count; i ++) {
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, top, UIScreenWidth - 40, 170)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:self.model.pictures[i]]];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        [header addSubview:imageView];
        top = imageView.bottom + 10;
        imageView.tag = 10000 + i;
        UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSelectedImageView:)];
        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:ges];
    }
    header.height = top + 10 + 49;

    self.header = header;
    self.tableView.tableHeaderView = header;
}
- (void)configBootomView{
    if (self.userModel.is_doctor) {
        self.tableView.height = UIScreenHeight - 49;
        STButton * contactButton = [[STButton alloc] initWithFrame:CGRectMake(0, [KLUtils tabBarTop], UIScreenWidth, 49)
                                                             title:@"立即指导"
                                                        titleColor:[UIColor whiteColor]
                                                         titleFont:18
                                                      cornerRadius:0
                                                   backgroundColor:KL_BlueBackGroundColor
                                                   backgroundImage:nil
                                                             image:nil];
        contactButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        __weak typeof(self) weakSelf =  self;
        [contactButton setClicAction:^(UIButton *sender) {
            [weakSelf onselctedContact];
        }];
        self.cotactButton = contactButton;
        self.cotactButton.hidden = NO;
        [self.view addSubview:contactButton];
    }

}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1 ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    [self tableView:tableView cellForRowAtIndexPath:indexPath];
    KLSuggestModel *suggestModel = self.dataSouce[indexPath.row];
    return suggestModel.height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 44;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * sectionHeader = [KLUtils headerViewWithTitle:@"专家指出:"];
    return sectionHeader;
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    KLWeekTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[KLWeekTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.suggestModel = self.dataSouce[indexPath.row];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark --Action Method
- (void)onselctedContact{
    KLWeekSuggestViewController * vc = [KLWeekSuggestViewController new];
    vc.model = self.model;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark --NetWork Method
- (void)sendWeekPeopleRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",serviceUrl,@"/api/doctor/illnessinfo"];
    if (self.model) {
        [dic setValue:self.model.illness_id forKey:@"illness_id"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 NSDictionary * dataDic =  responseObject[@"data"];
                                                 if ([dataDic.allKeys containsObject:@"info"]) {
                                                     self.model = [KLWeekModel mj_objectWithKeyValues:dataDic[@"info"]];
                                                     [self configSubView];
                                                 }
                                                 self.tableView.st_noreslutView.top =  self.header.height + 70;

                                                 if ([dataDic.allKeys containsObject:@"opinion"]) {
                                                     NSArray * array = dataDic[@"opinion"];
                                                     self.dataSouce = [NSMutableArray new];
                                                     for (NSDictionary * modelDic in array) {
                                                         KLSuggestModel * model = [KLSuggestModel mj_objectWithKeyValues:modelDic];
                                                         [self.dataSouce addObject:model];
                                                     }
                                                 }
                                                 [self.tableView reloadData];
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                  [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 [self.tableView.mj_header endRefreshing];
                                                
                                             }];
}
- (void)sendMyInfoRequest{

    [KLUtils fetch_MineInfoWithHandle:^(KLUserModel *userModel, STError *error) {
        self.userModel = userModel;
        [self configBootomView];
    }];
}
#pragma mark -图片查看
- (void)onSelectedImageView:(UIGestureRecognizer*)ges{
    UIImageView * imageView = (id)ges.view;
    NSMutableArray * dataSouce = [NSMutableArray new];
    for (NSString * url in self.model.pictures) {
        STUrlPhotoModel * model = [STUrlPhotoModel new];
        model.originImageUrl = url;
        model.thumbImageUrl = url;
        //        model.originImage = imageView.image;
        [dataSouce addObject:model];
    }
    NSInteger tag = imageView.tag - 10000;
    STPhotoUrlImageBrowerController *  vc = [[STPhotoUrlImageBrowerController alloc] initWithArray:dataSouce curentIndex:tag];
    vc.shouldHideBottomView = YES;
    [self presentViewController:vc animated:NO completion:nil];
}
@end
